<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath()}+'/'">
    <link rel="stylesheet" href="static/element/index.css"/>
</head>
<body>
<div id="app">
    <template>
        <div style="margin-bottom:18px;">
            <el-input style="width: 13%" v-model="queryForm.menuName" placeholder="菜单名称"></el-input>
            <el-button @click="submitQueryForm">查询</el-button>
            <el-button v-if="button.save" @click="openSaveDialog">新增</el-button>
        </div>

        <el-table :data="tableData" row-key="id" :row-style="{height:'40px'}" :cell-style="{padding:'0px'}"
                  style="font-size: 14px" border>
            <el-table-column prop="menuName" label="菜单名称"></el-table-column>
            <el-table-column prop="url" label="URL"></el-table-column>
            <el-table-column prop="type" label="类型" :formatter="formatType"></el-table-column>
            <el-table-column prop="sort" label="排序"></el-table-column>
            <el-table-column prop="remark" label="备注"></el-table-column>
            <el-table-column prop="createTime" label="创建时间"></el-table-column>
            <el-table-column label="操作" width="160">
                <template slot-scope="scope">
                    <el-button v-if="scope.row.type == 1 && button.save" type="text" @click="openSaveDialog(scope.row)">
                        新增
                    </el-button>
                    <el-button v-if="button.update" type="text" @click="openUpdateDialog(scope.row)">修改</el-button>
                    <el-button v-if="button.delete" type="text" @click="del(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog title="新增" width="90%" :visible.sync="saveDialog" :modal="false" :close-on-click-modal="false">
            <el-form :model="saveForm" ref="saveForm" :rules="rules" label-width="100px">
                <el-row>
                    <el-col :span="7">
                        <el-form-item label="菜单名称" prop="menuName">
                            <el-input v-model="saveForm.menuName"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="上级菜单" prop="parentId">
                            <el-cascader v-model="saveForm.parentId" :options="parentMenu"
                                         :props="{value:'id', label:'menuName', checkStrictly:true, expandTrigger:'hover', emitPath:false}"
                                         :show-all-levels="false" clearable></el-cascader>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="URL" prop="url">
                            <el-input v-model="saveForm.url"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="7">
                        <el-form-item label="类型" prop="type">
                            <el-select v-model="saveForm.type" placeholder="请选择" style="width:100%">
                                <el-option v-for="item in menuType" :key="item.value" :label="item.label"
                                           :value="item.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="排序" prop="sort">
                            <el-input-number v-model="saveForm.sort" :min="1" :max="99"
                                             style="width:100%"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="saveForm.remark"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="saveDialog = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="修改" width="90%" :visible.sync="updateDialog" :modal="false" :close-on-click-modal="false">
            <el-form :model="updateForm" ref="updateForm" :rules="rules" label-width="100px">
                <el-row>
                    <el-col :span="7">
                        <el-form-item label="菜单名称" prop="menuName">
                            <el-input v-model="updateForm.menuName"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="上级菜单" prop="parentId">
                            <el-cascader v-model="updateForm.parentId" :options="parentMenu"
                                         :props="{value:'id', label:'menuName', checkStrictly:true, expandTrigger:'hover', emitPath:false}"
                                         :show-all-levels="false" clearable></el-cascader>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="URL" prop="url">
                            <el-input v-model="updateForm.url"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="7">
                        <el-form-item label="类型" prop="type">
                            <el-select v-model="updateForm.type" placeholder="请选择" style="width:100%">
                                <el-option v-for="item in menuType" :key="item.value" :label="item.label"
                                           :value="item.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="排序" prop="sort">
                            <el-input-number v-model="updateForm.sort" :min="1" :max="99"
                                             style="width:100%"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="updateForm.remark"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="updateDialog = false">取 消</el-button>
                <el-button type="primary" @click="update">确 定</el-button>
            </div>
        </el-dialog>
    </template>
</div>
</body>
<script src="static/js/axios.min.js"></script>
<script src="static/js/api.js"></script>
<script src="static/js/vue.min.js"></script>
<script src="static/element/index.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
			return {
                tableData: [],
                saveDialog: false,
                updateDialog: false,
                button: {
                    save: false,
                    update: false,
                    delete: false
                },
                saveForm: {
                    menuName: '',
                    parentId: '',
                    type: '',
                    sort: 1,
                    url: '',
                    perms: '',
					icon: '',
                    remark: ''
                },
                updateForm: {
                	id: '',
                    menuName: '',
                    parentId: '',
                    type: '',
                    sort: 1,
                    url: '',
                    perms: '',
                    icon: '',
                    remark: ''
                },
                queryForm: {
                    menuName: ''
                },
                rules: {
                    menuName: [
                        {required: true, message: '请输入', trigger: 'blur'},
                        {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
                    ],
                    type: [
                        {required: true, message: '请选择', trigger: 'blur'}
                    ]
                },
                menuType: [{
                	value: '1',
                	label: '菜单'
                },{
                	value: '2',
                	label: '按钮'
                }],
                parentMenu: []
			}
        },
        mounted() {
        },
        methods: {
        	formatType: function(row, column, cellValue, index) {
                return row.type == 1 ? '菜单' : row.type == 2 ? '按钮' : '';
            },
            submitQueryForm() {
                let queryParam = {
                    menuName:this.queryForm.menuName
                };
                axios.post('sys/menu/list', queryParam)
                .then((res) => {
                    if (res.code == '0000') {
                        this.tableData = res.data;
                    }
                });
            },
            openSaveDialog(row) {
	           	this.saveDialog = true;
                if (this.$refs.saveForm !== undefined) {
                    this.$refs.saveForm.resetFields();
                }
                this.saveForm.parentId = row.id;
                let queryParam = {"type":"1"};
                axios.post('sys/menu/list', JSON.parse(JSON.stringify(queryParam)))
                .then((res) => {
                    if (res.code == '0000') {
                        this.parentMenu = res.data;
                    }
                });
            },
            save() {
            	this.$refs.saveForm.validate((valid) => {
                    if (valid) {
                        this.saveDialog = false;
                        let saveParam = this.saveForm;
                        axios.post('sys/menu/save', JSON.parse(JSON.stringify(saveParam)))
                        .then((res) => {
                            if (res.code == '0000') {
                                this.submitQueryForm();
                                this.$message({
                                    type: 'success',
                                    message: '新增成功'
                                });
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },
            del(row) {
                this.$confirm('您确定删除吗', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                	axios.post('sys/menu/delete/' + row.id)
                    .then((res) => {
                        if (res.code == '0000') {
                            this.submitQueryForm();
                            this.$message({
                                type: 'success',
                                message: '删除成功'
                            });
                        }
                    });
                }).catch(() => {
                });
            },
            openUpdateDialog(row) {
                this.updateDialog = true;
                axios.get('sys/menu/get/' + row.id)
                .then((res) => {
                    if (res.code == '0000') {
						this.updateForm.id = res.data.id;
                        this.updateForm.menuName = res.data.menuName;
                        this.updateForm.parentId = res.data.parentId;
                        this.updateForm.type = res.data.type;
                        this.updateForm.sort = res.data.sort;
                        this.updateForm.url = res.data.url;
                        this.updateForm.perms = res.data.perms;
                        this.updateForm.icon = res.data.icon;
                        this.updateForm.remark = res.data.remark;
                    } else {
						this.$message.error(res.message);
					}
                });
                let queryParam = {"type":"1"};
                axios.post('sys/menu/list', JSON.parse(JSON.stringify(queryParam)))
                .then((res) => {
                    if (res.code == '0000') {
                        this.parentMenu = res.data;
                    }
                });
            },
            update(formName) {
            	this.$refs.updateForm.validate((valid) => {
                    if (valid) {
                        this.updateDialog = false;
                        let updateParam = this.updateForm;
                        axios.post('sys/menu/update', JSON.parse(JSON.stringify(updateParam)))
                        .then((res) => {
                            if (res.code == '0000') {
                                this.submitQueryForm();
                                this.$message({
                                    type: 'success',
                                    message: '修改成功'
                                });
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    } else {
                        return false;
                    }
                });
            }
        },
        created() {
			this.submitQueryForm();
			let sysButtons = JSON.parse(localStorage.getItem("jsbb-sysButtons"));
			this.button.save = sysButtons.indexOf("sys/menu/save") > -1;
			this.button.update = sysButtons.indexOf("sys/menu/update") > -1;
			this.button.delete = sysButtons.indexOf("sys/menu/delete") > -1;
        }
    });
</script>
</html>
